package com.windo.helloBubble;

import com.windo.helloBubble.objects.Bubbles;
import com.windo.helloPad.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class main extends Activity {
	
	protected static final int GUIUPDATEIDENTIFIER = 0x101;
	private static Bubbles bubblePool;
	private Button toSwipeView;
	private Thread animator;
	
	Handler myGUIUpdateHandler = new Handler() {		 
        // @Override
        public void handleMessage(Message msg) {
        	switch (msg.what) {
            	case main.GUIUPDATEIDENTIFIER:
            		bubblePool.moveBubbles();
            		//bubblePool.invalidate();
                    break;
                }
            super.handleMessage(msg);
        }
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //get the frameLayout
        FrameLayout mainViews = (FrameLayout) findViewById(R.id.main_view);
        bubblePool = new Bubbles(this);
        mainViews.addView(bubblePool);
        
        //after the bubblePool is setup, we can start the thread
        animator = new Thread(new Driver());
        animator.start();
        
        toSwipeView = (Button) findViewById(R.id.btn_swipeView);
        toSwipeView.setOnClickListener(toSwipeViewBtnListener);
        
        mainViews.setOnTouchListener(new View.OnTouchListener() {
            @Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
            	float x = e.getX();
            	float y = e.getY();
            	Log.v("DSMMM", "a click AT -> " + x + ":" + y + " and we are adding a new bubble+");
            	if(!bubblePool.removeBubble(x, y)){
            		bubblePool.addBubble(x, y);
            	}
            	return false;
			}
        });
    }
    
    /**      Listeners      */
    private OnClickListener toSwipeViewBtnListener = new OnClickListener() {
		public void onClick(View v) {
			Intent myIntent = new Intent(v.getContext(), SwipeView.class);
			startActivity(myIntent);
		}
	};
  //private thread class
	class Driver implements Runnable{
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(true){
				Message message = new Message();
				message.what = main.GUIUPDATEIDENTIFIER;
				main.this.myGUIUpdateHandler.sendMessage(message);
				try{
					//Log.v("DSMMM", "move move move!");
					Thread.sleep(15); // a 10th of a second
					//main.bubblePool.invalidate();
				}catch(InterruptedException e){
					Thread.currentThread().interrupt();
				}
			}
		}
	}
}